// This file contains order-related API functions
import type { 
  PayOrderData, PayOrderResponses,
  InitCourseStockData, InitCourseStockResponses,
  CreateOrderData, CreateOrderResponses,
  CancelOrderData, CancelOrderResponses,
  GetUserOrdersData, GetUserOrdersResponses
} from './orderTypes';
import { client as _heyApiClient } from '../../api/client.gen';
import type { Options } from '../common/apiTypes';

/**
 * 订单支付
 * 对指定订单进行支付操作
 */
export const payOrder = <ThrowOnError extends boolean = false>(options: Options<PayOrderData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).post<PayOrderResponses, unknown, ThrowOnError>({
        url: '/api/order/pay',
        ...options,
        headers: {
            'Content-Type': 'application/json',
            ...options.headers
        }
    });
};

/**
 * 初始化课程库存
 * 开发测试用：初始化指定课程的库存到Redis缓存
 */
export const initCourseStock = <ThrowOnError extends boolean = false>(options: Options<InitCourseStockData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).post<InitCourseStockResponses, unknown, ThrowOnError>({
        url: '/api/order/init-stock',
        ...options
    });
};

/**
 * 创建订单
 * 根据课程ID创建新的订单记录
 */
export const createOrder = <ThrowOnError extends boolean = false>(options: Options<CreateOrderData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).post<CreateOrderResponses, unknown, ThrowOnError>({
        url: '/api/order/create',
        ...options,
        headers: {
            'Content-Type': 'application/json',
            ...options.headers
        }
    });
};

/**
 * 取消订单
 * 取消未支付的订单
 */
export const cancelOrder = <ThrowOnError extends boolean = false>(options: Options<CancelOrderData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).post<CancelOrderResponses, unknown, ThrowOnError>({
        url: '/api/order/cancel',
        ...options,
        headers: {
            'Content-Type': 'application/json',
            ...options.headers
        }
    });
};

/**
 * 查询用户订单列表
 * 获取当前登录用户的所有订单信息
 */
export const getUserOrders = <ThrowOnError extends boolean = false>(options?: Options<GetUserOrdersData, ThrowOnError>) => {
    return (options?.client ?? _heyApiClient).get<GetUserOrdersResponses, unknown, ThrowOnError>({
        url: '/api/order/list',
        ...options
    });
};